package com.cman777.springc.common.helper;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author chenzhicong
 * @time 2019/11/9 17:46
 * @description
 */
@Slf4j
public class MapUtil {
    /**
     * 转换map
     *
     * @param paramMap
     * @return
     */
    @NotNull
    public static Map<String, String> getStringMap(Map<String, String[]> paramMap) {
        if ((paramMap == null) || (paramMap.isEmpty())) {
            return Collections.emptyMap();
        }
        Map<String, String> result = new HashMap<>(32);

        for (Map.Entry<String, String[]> entry : paramMap.entrySet()) {
            String[] value = entry.getValue();
            if (value == null) {
                continue;
            }
            String newValue = StringUtils.join(value, ",");
            result.put(entry.getKey(), newValue);
        }
        return result;
    }
    public static Map<String, Object> objectToMap(Object obj) {
        if (obj == null) {
            return null;
        }
        Collection<Field> fields = BeanUtil.getDeclaredFields(obj.getClass());
        Map<String, Object> map = new HashMap<>(fields.size());
        for (Field field : fields) {
            map.put(field.getName(), BeanUtil.getFieldValueQuietly(field, obj));
        }
        return map;
    }
    public static <T> T toBean(Map<String, Object> map, Class<T> clazz) {
        T obj = null;
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
            obj = clazz.newInstance(); // 创建 JavaBean 对象

            // 给 JavaBean 对象的属性赋值
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (int i = 0; i < propertyDescriptors.length; i++) {
                PropertyDescriptor descriptor = propertyDescriptors[i];
                String propertyName = descriptor.getName();
                if (map.containsKey(propertyName)) {
                    // 下面一句可以 try 起来，这样当一个属性赋值失败的时候就不会影响其他属性赋值。
                    Object value = map.get(propertyName);
                    if ("".equals(value)) {
                        value = null;
                    }
                    Object[] args = new Object[1];

                    if (value instanceof Long) {
                        args[0] = Integer.parseInt(value.toString());
                    } else {
                        args[0] = value;
                    }

                    try {
                        descriptor.getWriteMethod().invoke(obj, args);
                    } catch (InvocationTargetException e) {
                        System.out.println("字段映射失败");
                    }
                }
            }
        } catch (IllegalAccessException e) {
            log.error("实例化 JavaBean 失败");
        } catch (IntrospectionException e) {
            log.error("分析类属性失败");
        } catch (IllegalArgumentException e) {
            log.error("字段类型映射错误");
        } catch (InstantiationException e) {
            log.error("实例化 JavaBean 失败");
        }
        return (T) obj;
    }





}
